class PriceUpdate {
    constructor ({url, method}) {
        this.url = url;
        this.method = method;
        this.model = null;
        // event
        this.bindEvent();
    }
    updateTpl (original_price, special_price) {
        let isSpecial = !!special_price ? 'checked' : '';

        let tpl =
            `<div class="price-update">
                <form action="${this.url}" method="${this.method}">
                    <div class="price-update-block">
                        <p class="price-update__title">请输入价格</p>
                        <div class="price-update-input">
                            <label>商品正价</label>
                            <input class="input" type="number" required name="original_price" value="${original_price}">
                        </div>
                        <div class="price-update-input">
                            <label class="park-form">
                                <span class="park-form__text park-form__text--left">特价</span>
                                <input name="isSpecial" type="checkbox" class="park-form__radio" ${isSpecial}>
                                <span class="park-form__radioInput"></span>
                            </label>
                            <input id="specialInput" class="input" type="number" value="${special_price}" name="special_price">
                        </div>
                        <div class="price-update-edit">
                            <div id="cancelUpdate" class="price-update-btn price-update-btn__cancel">取消</div>
                            <button type="submit" class="price-update-btn price-update-btn__confirm">确定</button>
                        </div>
                    </div>
                </form>
            </div>`;
        return tpl;
    }
    cancelUpdate () {
        $(document).on('click', '#cancelUpdate', () => {
            this.hideMode();
        });
    }
    showModel (original_price, special_price) {
        this.model = $(this.updateTpl(original_price, special_price));
        $('body').append(this.model);
        this.initSpecialInput();
        // event
        this.changeCheck();
    }
    hideMode () {
        this.model.remove();
    }
    initSpecialInput () {
        let isShow = this.model.find('[type="checkbox"]').is(':checked');
        $('#specialInput').toggle(isShow);
    }
    changeCheck () {
        this.model.find('[type="checkbox"]').on('change', () => {
            this.initSpecialInput();
        });
    }
    bindEvent () {
        this.cancelUpdate();
    }
}